﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace AliyunOpenSearch4Net.Base
{
    class BooleanConverter : JsonConverter
    {
        private static Type _booleanType = typeof(bool);

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(bool);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.ValueType == _booleanType)
                return (bool)reader.Value;

            var strValue = reader.Value.ToString();

            if (strValue == "1")
                return true;

            if ("true".Equals(strValue, StringComparison.OrdinalIgnoreCase))
                return true;

            return false;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
